package org.example.learn.javaagent.threadlocal_link_to_track;

import net.bytebuddy.asm.Advice;

import java.util.UUID;

// 链路追踪 信息通知
public class TrackAdvice {

    // 进入方法事件
    @Advice.OnMethodEnter()
    public static void enter(@Advice.Origin("#t") String className,@Advice.Origin("#m") String methodName){
        String linkId = TrackManager.getCurrentSpan();
        if (null == linkId) {
            linkId = UUID.randomUUID().toString();
            TrackContext.setLinkId(linkId);
        }
        String entrySpan = TrackManager.createEntrySpan();
        System.out.println("链路追踪：" + entrySpan + " " + className + " " + methodName);
    }

    // 退出方法事件
    @Advice.OnMethodExit()
    public static void exit(@Advice.Origin("#t") String className,@Advice.Origin("#m") String methodName){
        TrackManager.getExitSpan();
    }
}
